#!/bin/bash

get_list() {
    local lst=""

    if [ -f "$1" ]; then
        lst=$(cat $1)
    else
        lst=$1
    fi

    echo $lst
}

# find_sig(sigs.yaml, pkglist)
#   Find the sig group of the package
find_sig() {
    printf "%-30s %s\n" SIG PKG
    echo "---------------------------------------"
    for pkg in $2; do
        sig=$(grep -E "\- name|/${pkg}\$" $1 | grep -B1 -E "/${pkg}\$" | head -n 1 | grep "\- name" | \
            awk -F ':' '{print $2}' | awk '{print $1}')
        if [ -z "$sig" ]; then 
            sig="Not-Found"
        fi
        printf "%-30s %s\n" $sig $pkg
    done | tee sigs_result.txt | sort
}

find_sig $1 "$(get_list "$2")"

